﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Drawing;


public partial class Admin_EditUsers : System.Web.UI.Page
{
    string[] s; 
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }


    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int vis = 0;
        PnlResetPass.Visible = false;
        PnEdit.Visible = false;
        DataTable dt = new DataTable();
        dt.Columns.Add("نام");
        dt.Columns.Add("نام خانوادگی");
        dt.Columns.Add("آدرس");
        dt.Columns.Add("شماره تلفن");
        dt.Columns.Add("سطح دسترسی");
        dt.Columns.Add("آدرس ایمیل");
        dt.Columns.Add("موبایل");
        dt.Columns.Add("Username");
        dt.Columns.Add("UserId");
        if (RdRoles.SelectedItem.Value == "MExpert")
          s = Roles.GetUsersInRole("AdminExpert");
           else if(RdRoles.SelectedItem.Value == "Fin")
             s = Roles.GetUsersInRole("Finance");
             else if (RdRoles.SelectedItem.Value == "Exp")
               s = Roles.GetUsersInRole("Expert");
                else if (RdRoles.SelectedItem.Value == "Secr")    
                   s = Roles.GetUsersInRole("Secret");
        if (s.Length != 0)
        {
            GrdListUser.Visible = true;
            foreach (string s1 in s)
            {
                PnlResetPass.Visible = false;
                PnEdit.Visible = false;
                LblSta1.Text = "";
                LblRAdio.Visible = false;
                ProfileCommon p = Profile.GetProfile(s1);
                if (p.Common.Status == "On")
                {
                    DataRow dr;
                    dr = dt.NewRow();
                    dr["نام"] = p.Common.FirstName;
                    dr["نام خانوادگی"] = p.Common.LastName;
                    dr["آدرس"] = p.Common.Address;
                    dr["شماره تلفن"] = p.Common.Phone;
                    dr["سطح دسترسی"] = p.Common.Category;
                    dr["آدرس ایمیل"] = p.Common.Email;
                    dr["موبایل"] = p.Common.Mobile;
                    dr["Username"] = p.Common.username;
                    dr["UserId"] = p.Common.CategoryID;
                    dt.Rows.Add(dr);
                    GrdListUser.DataSource = dt;
                    GrdListUser.DataBind();
                    PnlShowData.Visible = true;
                    pnlData.Visible = true;
                    vis++;
                }
               
               
            }

            if (vis == 0) {
                LblRAdio.Visible = true;
                LblRAdio.Text = "هیچ کاربر فعال در این سطح تعریف نشده";
            
            }
        }
        else
        {
            GrdListUser.Visible = false;
            LblRAdio.Visible = true;
            LblRAdio.Text = "هیچ کاربر فعال در این سطح تعریف نشده";

        }
        }
       
   protected void GrdListUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        int Roid = Convert.ToInt32(e.CommandArgument);
        //string s = (GrdListUser.DataKeys[Roid].ToString());
       string usern = GrdListUser.Rows[Roid].Cells[2].Text;
       MembershipUser user = Membership.GetUser(usern);
       ProfileCommon p = Profile.GetProfile(usern);
        if (Membership.GetUser(usern) != null)
        {
           
            Session["name"] = p.UserName;
            switch (e.CommandName)
            {
                case "Revise":
                    PnlShowData.Visible = false;
                    PnEdit.Visible = true;
                    PnlResetPass.Visible = false;
                    LblSta1.Visible = false;
                    Txname.Text = p.Common.FirstName;
                    Txfamil.Text = p.Common.LastName;
                    TxEmail.Text = p.Common.Email;
                    Txadd.Text = p.Common.Address;
                    TxMobil.Text = p.Common.Mobile;
                    Txpho.Text = p.Common.Phone;
                    Txuser.Text = p.Common.username;
                    GrdListUser.DataBind();
                    break;
                case "ResetPass":
                    PnlResetPass.Visible = true;
                    PnEdit.Visible = false;
                    LblSta1.Visible = false;
                    Txusername.Text = p.Common.username;
                    break;
                case "Del":
                             
                    //if (user.IsApproved == true)
                    //{
                    PnlResetPass.Visible = false;
                    PnEdit.Visible = false;
                    p.Common.Status="Off";
                    p.Save();
                        user.IsApproved = false;
                        Membership.UpdateUser(user);
                        //Membership.DeleteUser(user.UserName);
                        GrdListUser.Rows[Roid].BackColor = Color.Gray;
                        LblSta1.Text = "کاربر غیر فعال کردید";
                    
                    //else {
                    //    user.IsApproved = true;
                    //    ProfileCommon p1 = Profile.GetProfile(Session["UserName"].ToString());
                    //    p1.Common.Status = "On";
                    //    Membership.UpdateUser(user);
                    //    GrdListUser.Rows[Roid].BackColor = Color.White;
                    //    LblSta1.Text = "کاربر فعال کردید";
                    //}
                        break;
            }

        }
        else {
            string display = "نام کاربری تعریف نشده";
            ClientScript.RegisterStartupScript(this.GetType(), "اعلان ها ", "alert('" + display + "');", true);
        }
    }
    protected void GrdListUser_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GrdListUser.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        GrdListUser.DataBind();
    }

    protected void   GrdListUser_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[6].Attributes.Add("onclick", "return confirm('آيا از حذف کردن کاربر مطمئن هستيد؟');");
        }
    }


    protected void BtnEdit_Click(object sender, EventArgs e)
    {
       
        ProfileCommon p = Profile.GetProfile(Session["name"].ToString());
        p.Common.FirstName = Txname.Text;
        p.Common.LastName = Txfamil.Text;
        p.Common.Mobile = TxMobil.Text;
        p.Common.Phone = Txpho.Text;
        p.Common.Email = TxEmail.Text;
        p.Common.Address = Txadd.Text;
        p.Save();
        Status.Text = "تغییرات با موفقیت انجام شد";
        BtnEdit.Enabled = false;
        GrdListUser.DataBind();    
    }


       
    protected void BtnRevise_Click(object sender, EventArgs e)
    {
        if (Membership.GetUser(Session["name"].ToString()) != null)
        {

            MembershipUser user = Membership.GetUser(Session["name"].ToString());
            string username = user.UserName;

            string Oldpassword = user.ResetPassword();
            bool result = user.ChangePassword(Oldpassword, TxPass.Text);
            LblSta1.Text = "تغییرات با موفقیت انجام شد";

        }
        else
        {

            LblSta1.Text = "نام کاربری وجود ندارد";
        }
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect(@"~/Admin/EditUsers.aspx");
    }
   
}


